package me.pplay.playtalk.activity;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.protocol.DataCallBack;
import me.pplay.playtalk.protocol.TpAuth;
import me.pplay.playtalk.protocol.bean.RspData;
import me.pplay.playtalk.util.LogManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

public class SigninGetPwdMethodActivity extends BaseActivity {
	private Spinner spinner;
	private ArrayAdapter<String> adapter;
	private String[] m;
	private ImageButton signin_gewpwd_method_btn;
	private EditText usernameEdit;
	private int selectIndex = 0;
	private TpAuth tpAuth;
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case TpAuth.AUTH_GETPWDPROTECTION_ME_OK:
				LogManager.LogShow("SigninGetPwdMethodActivity", "handleMessage AUTH_GETPWDPROTECTION_ME_OK");
				if (selectIndex != 0){
					String username = usernameEdit.getText().toString().trim();
					Intent intent = new Intent(SigninGetPwdMethodActivity.this, SigninGetPwdSecretQuestionActivity.class);
					intent.putExtra("username", username);
					startActivity(intent);
				}
				break;
			case TpAuth.AUTH_GETPWDPROTECTION_ME_ERR: {
				LogManager.LogShow("SigninGetPwdMethodActivity", "handleMessage AUTH_GETPWDPROTECTION_ME_ERR");
				// loadDialog.cancel();
				RspData rspdata = (RspData) msg.obj;
				showToast(rspdata.getString() == null ? "Unkown err" : rspdata.getString());
			}
				break;
			case TpAuth.AUTH_GETPWDPROTECTION_ME_TIMEOUT:
				LogManager.LogShow("SigninGetPwdMethodActivity", "handleMessage AUTH_GETPWDPROTECTION_ME_TIMEOUT");
				// loadDialog.cancel();
				showToast("Time out");
				break;
			case TpAuth.AUTH_GETPWD_OK: {
				LogManager.LogShow("SigninGetPwdMethodActivity", "handleMessage AUTH_GETPWD_OK");
				RspData rspdata = (RspData) msg.obj;
				showToast(rspdata.getString() == null ? "Send email done" : rspdata.getString());
			}
				break;
			case TpAuth.AUTH_GETPWD_ERR: {
				LogManager.LogShow("SigninGetPwdMethodActivity", "handleMessage AUTH_GETPWD_ERR");
				RspData rspdata = (RspData) msg.obj;
				showToast(rspdata.getString() == null ? "Send password unkown err" : rspdata.getString());
			}
				break;
			case TpAuth.AUTH_GETPWD_TIMEOUT:
				LogManager.LogShow("SigninGetPwdMethodActivity", "handleMessage AUTH_GETPWD_TIMEOUT");
				showToast("Send Password time out");
				break;
			default:
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_signin_getpwd_method);
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		tpAuth = appState.getTpAuth();
		usernameEdit = (EditText) findViewById(R.id.signin_getpwd_username);
		signin_gewpwd_method_btn = (ImageButton) findViewById(R.id.signin_gewpwd_method_btn);
		signin_gewpwd_method_btn.setOnClickListener(new BtnSelectedListener());
		spinner = (Spinner) findViewById(R.id.signin_gewpwd_spinner);
		m = getResources().getStringArray(R.array.array_signin_gewpwd_method);
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m);
		// 设置下拉列表的风格
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// 将adapter 添加到spinner中
		spinner.setAdapter(adapter);
		// 添加事件Spinner事件监听
		spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
		// 设置默认值
		spinner.setVisibility(View.VISIBLE);
	}

	// 使用数组形式操作
	class SpinnerSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			selectIndex = arg2;
		}

		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}

	class BtnSelectedListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			LogManager.LogShow("SigninGetPwdMethodActivity", "BtnSelectedListener selectIndex= " + selectIndex);
			String username = usernameEdit.getText().toString().trim();
			if (username.equals("")) {
				showToast(getString(R.string.username_isnull));
				return;
			}
			if (selectIndex == 0) {// email
				String me_email = tpAuth.tp_auth_get_pwdprotection_me_email();
				if (me_email != null&&me_email.length()>0) {
					getPwdBymail(me_email);
				} else {
					getSelfEmailAndQuestion();
				}
			} else {// secret question
				String me_qstr = tpAuth.tp_auth_get_pwdprotection_me_qstr();
				if (me_qstr != null&&me_qstr.length()>0) {
					Intent intent = new Intent(SigninGetPwdMethodActivity.this, SigninGetPwdSecretQuestionActivity.class);
					intent.putExtra("username", username);
					startActivity(intent);
				} else {
					getSelfEmailAndQuestion();
				}
			}
		}

	}

	private void getSelfEmailAndQuestion() {
		String username = usernameEdit.getText().toString().trim();
		tpAuth.tp_auth_get_pwdprotection_me(username, new DataCallBack() {

			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				// TODO Auto-generated method stub
				Message msg = new Message();
				msg.what = result;
				msg.obj = resultdata;
				LogManager.LogShow("SigninGetPwdMethodActivity", "Callback" + result);
				if (TpAuth.AUTH_GETPWDPROTECTION_ME_OK == result) {
					LogManager.LogShow("SigninGetPwdMethodActivity", "AUTH_GETPWDPROTECTION_ME_OK");
					if (selectIndex == 0) {
						String me_email = tpAuth.tp_auth_get_pwdprotection_me_email();
						if (me_email != null&&me_email.length()>0) {// email
							getPwdBymail(me_email);
						} else {
							LogManager.LogShow("SigninGetPwdMethodActivity", "No Email");
							RspData rspData = new RspData();
							rspData.setString("No Email");
							msg.what = TpAuth.AUTH_GETPWDPROTECTION_ME_ERR;
							handler.sendMessage(msg);
							return;
							// 提示用户email没有input
						}
					}else{
						String me_qstr = tpAuth.tp_auth_get_pwdprotection_me_qstr();
						
						if (me_qstr != null&&me_qstr.length()>0) {// 
							LogManager.LogShow("SigninGetPwdMethodActivity", "me_qstr = "+me_qstr
									+"me_qstr len"+me_qstr.length());
						} else {
							LogManager.LogShow("SigninGetPwdMethodActivity", "No Secret Question");
							RspData rspData = new RspData();
							rspData.setString("No Secret Question");
							msg.what = TpAuth.AUTH_GETPWDPROTECTION_ME_ERR;
							handler.sendMessage(msg);
							return;
						}
					}
					handler.sendMessage(msg);
				} else if (TpAuth.AUTH_GETPWDPROTECTION_ME_ERR == result) {
					LogManager.LogShow("SigninGetPwdMethodActivity", "AUTH_GETPWDPROTECTION_ME_ERR");
					handler.sendMessage(msg);
				} else if (TpAuth.AUTH_GETPWDPROTECTION_ME_TIMEOUT == result) {
					LogManager.LogShow("SigninGetPwdMethodActivity", "AUTH_GETPWDPROTECTION_ME_TIMEOUT");
					handler.sendMessage(msg);
				}
			}
		});
	}

	private void getPwdBymail(String mailbox) {
		String user = usernameEdit.getText().toString().trim();
		tpAuth.tp_auth_get_pwdbymail(user, "", mailbox, new DataCallBack() {

			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				// TODO Auto-generated method stub
				Message msg = new Message();
				msg.what = result;
				msg.obj = resultdata;
				LogManager.LogShow("SigninGetPwdMethodActivity", "Callback" + result);
				if (TpAuth.AUTH_GETPWD_OK == result) {
					LogManager.LogShow("SigninGetPwdMethodActivity", "AUTH_GETPWD_OK");
					handler.sendMessage(msg);
				} else if (TpAuth.AUTH_GETPWD_ERR == result) {
					LogManager.LogShow("SigninGetPwdMethodActivity", "AUTH_GETPWD_ERR");
					handler.sendMessage(msg);
				} else if (TpAuth.AUTH_GETPWD_TIMEOUT == result) {
					LogManager.LogShow("SigninGetPwdMethodActivity", "AUTH_GETPWD_TIMEOUT");
					handler.sendMessage(msg);
				}
			}
		});
	}

//	private void showToast(String str) {
//		Toast.makeText(SigninGetPwdMethodActivity.this, str, Toast.LENGTH_SHORT).show();
//	}
}
